#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <unordered_set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int N=5e3+10;
int a[N];
pii b[N];
void solve()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i);
	
	ll ans=0;
	for(int i=1;i<=n;i++)
	{
	    int tail=0;
		for(int j=i;j<=n;j++)
		{
			pii p={a[j],a[j]};
		    while(tail && a[j]<b[tail].second)
		    {
		    	p.second=max(b[tail].second,p.second);
		    	tail--;
			}
		    tail++;
		    b[tail]=p;
			ans=ans+j-i-tail+1;
		}
	}
	cout<<ans<<endl;
	
}

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		solve();
	}

	return 0;
}

